﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class InsertUser

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.DepartmentRepository = PhDir.Main.Instance.DepartmentRepository
        Me.UserRepository = PhDir.Main.Instance.UserRepository

        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                     ControlStyles.UserPaint Or
                     ControlStyles.AllPaintingInWmPaint, True)
    End Sub

    Private Property DepartmentRepository As DepartmentRepository
    Private Property UserRepository As UserRepository
    Private Property Departments As List(Of Department)

    Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
        If Me.DepartmentComboBox.Items.Count <> 0 Then
            Me.DepartmentComboBox.SelectedIndex = 0
        End If
        Me.NameTextBox.Text = String.Empty
        Me.PhoneMaskedBox.Text = String.Empty
    End Sub

    ''' <summary>
    ''' Populate Departments into DepartmentComboBox when its visibility is true
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub InsertUser_VisibleChanged(sender As Object, e As EventArgs) Handles MyBase.VisibleChanged
        If Me.Visible = True Then
            Me.Departments = Me.DepartmentRepository.SelectAll()

            Me.DepartmentComboBox.DataSource = (From department In Me.Departments _
                                               Select department.DepartmentName).ToList()
        End If

        Me.ResetButton_Click(Me, EventArgs.Empty)
    End Sub

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

        If Me.ValidateChildren() = True Then
            Dim isSuccess = False
            Dim department = New Department With
                             {
                                 .DepartmentName = Me.DepartmentComboBox.SelectedItem
                             }
            Dim user = New User With
                       {
                           .Department = department,
                           .UserName = Me.NameTextBox.Text
                       }
            Dim phone = New Phone With
                        {
                            .User = user,
                            .PhoneNumber = Me.PhoneMaskedBox.Text
                        }
            isSuccess = Me.UserRepository.Insert(phone)

            If isSuccess = True Then
                MessageBox.Show("Operation Success")
            Else
                MessageBox.Show("Operation Failed")
            End If
        End If

    End Sub

    Private Sub NameTextBox_Validated(sender As Object, e As EventArgs) Handles NameTextBox.Validated
        Me.NameTextBoxErrorProvider.Clear()
    End Sub

    Private Sub NameTextBox_Validating(sender As Object, e As CancelEventArgs) Handles NameTextBox.Validating
        If Me.NameTextBox.Text = String.Empty Then
            Me.NameTextBoxErrorProvider.SetError(Me.NameTextBox, "Name is required")
            'e.Cancel = True
        End If
    End Sub

    Private Sub PhoneMaskedBox_Validated(sender As Object, e As EventArgs) Handles PhoneMaskedBox.Validated
        Me.PhoneErrorProvider.Clear()
    End Sub

    Private Sub PhoneMaskedBox_Validating(sender As Object, e As CancelEventArgs) Handles PhoneMaskedBox.Validating
        If RegexUtil.Match(RegexPattern.PhoneNumber, Me.PhoneMaskedBox.Text) = False Then
            Me.PhoneErrorProvider.SetError(Me.PhoneMaskedBox, "Invalid Phone Number")
            'e.Cancel = True
        End If
    End Sub
End Class
